package com.air.antispider.stream.dataprocess.businessprocess

import java.util.regex.{Matcher, Pattern}

import com.air.antispider.stream.commom.util.decode.MD5

/**
  * 数据脱敏
  */
object EncryptedMessage {
  /**
    * 对身份证加密
    * @param encryptedPhoneMessage
    * @return
    */
  def encryptedID(encryptedPhoneMessage: String): String = {
    //创建加密对象
    val md5 = new MD5
    //创建正则对象匹配消息
    val idPattern: Pattern = Pattern.compile("(\\d{18})|(\\d{17}(\\d|X|x))|(\\d{15})")
    val idMatcher: Matcher = idPattern.matcher(encryptedPhoneMessage)

    var result = encryptedPhoneMessage
    while (idMatcher.find()) {
      val id: String = idMatcher.group()
      //替换加密
      val encryptedID: String = md5.getMD5ofStr(id)
      result = result.replaceAll(id,encryptedID)
    }
    result
  }

  /**
    * 对手机号加密
    *
    * @param message
    * @return
    */
  def encryptedPhone(message: String): String = {
    //创建加密对象
    val md5 = new MD5
    //创建正则对象去匹配手机号
    val phonePattern: Pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0-9])|(18[0,5-9]))\\d{8}")
    //matcher对象是对结果的封装
    val phoneMatcher: Matcher = phonePattern.matcher(message)

    var result: String = message
    while (phoneMatcher.find()) {
      val phone: String = phoneMatcher.group()
      //加密
      val encryptedPhone: String = md5.getMD5ofStr(phone)
      //将原数据中的手机号替换为加密后的手机号
      result = result.replaceAll(phone,encryptedPhone)
    }
    result

  }

}
